package com.qf.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Component
public class EmailUtils {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired(required = false)
    JavaMailSender javaMailSender;


    public Map send(String email){
            //创建SimpleMailMessage
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
         simpleMailMessage.setTo(email);
        simpleMailMessage.setSubject("千峰邮箱验证");
        simpleMailMessage.setFrom(from);
        //设置邮箱内容
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0;i<4;i++){
            int i1 = new Random().nextInt(10);
            stringBuffer.append(i1);
        }
       simpleMailMessage.setText(stringBuffer.toString());
        javaMailSender.send(simpleMailMessage);
        Map map=new HashMap();
        map.put("code",stringBuffer.toString());
        return map;
    }
}
